<?php
namespace app\course\controller;

use think\Controller;
use app\course\model\User;
use app\course\model\course as Modelcourse;
use app\course\model\tc;
use app\course\model\sc;

class Course extends controller
{
    public function courselist(){
        $user_Id = $_GET["userId"];
        $user_status = $_GET["userstatus"];
        $list = new User();
        if($user_status==0){
            $data = $list->SC($user_Id);
        }else{
            $data = $list->TC($user_Id);
        }
        

        die(json_encode($data));
    }

    public function addcourse(){
        $inputText = file_get_contents("php://input");
        $inputData = json_decode($inputText,true);

        $T_Id = $inputData["tId"];
        $courseId = $inputData["cId"];
        $courseName = $inputData["cname"];

        $addcourse = new Modelcourse();
        $add = $addcourse->addcourse($courseId,$courseName);
        $newcourse = $addcourse->newcourse();

        $C_Id = $newcourse->toArray()["0"]["Id"];
        $addtc = new tc();
        $ad = $addtc->addcourse($C_Id,$T_Id); 
        
        if($add === false || $ad === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success",
            ];
        }

        die(json_encode($retData));
        return $retData;
    }

    public function joincouselist(){
        $list = new Modelcourse();
        $data = $list->courselist();

        die(json_encode($data));
    }

    public function joincouse(){
        $inputText = file_get_contents("php://input");
        $inputData = json_decode($inputText,true);

        $S_Id = $inputData["sId"];
        $courseId = $inputData["cId"];

        $joincourse = new sc();
        $join = $joincourse->joincourse($courseId,$S_Id);

        if($join === false){
            $retData = [
                "status"=>"fail",
                "message" =>"fail"
            ];
        }else{
            $retData = [
                "status"=>"success",
                "message" =>"success",
            ];
        }

        die(json_encode($retData));
        return $retData;
    }
}
?>